import java.util.Random;
import java.util.concurrent.*;

public class Test {
        public static void main(String[] args) throws Exception {
                // Fork/Join任务的原理：判断一个任务是否足够小，如果是，直接计算，否则，就分拆成几个小任务分别计算。这个过程可以反复“裂变”成一系列小任务
                // [参考](https://liaoxuefeng.com/books/java/threading/fork-join/index.html)
                // 创建2000个随机数组成的数组:
                long[] array = new long[2000];
                long expectedSum = 0;
                for (int i = 0; i < array.length; i++) {
                        array[i] = random();
                        expectedSum += array[i];
                }
                System.out.println("Expected sum: " + expectedSum);
                // fork/join:
                ForkJoinTask<Long> task = new SumTask(array, 0, array.length);
                long startTime = System.currentTimeMillis();
                Long result = ForkJoinPool.commonPool().invoke(task);
                long endTime = System.currentTimeMillis();
                System.out.println("Fork/join sum: " + result + " in " + (endTime - startTime) + " ms.");
        }

        static Random random = new Random(0);

        static long random() {
                return random.nextInt(10000);
        }
}

class SumTask extends RecursiveTask<Long> {
        static final int THRESHOLD = 500;
        long[] array;
        int start;
        int end;

        SumTask(long[] array, int start, int end) {
                this.array = array;
                this.start = start;
                this.end = end;
        }

        @Override
        protected Long compute() {
                if (end - start <= THRESHOLD) {
                        // 如果任务足够小,直接计算:
                        long sum = 0;
                        for (int i = start; i < end; i++) {
                                sum += this.array[i];
                                // 故意放慢计算速度:
                                try {
                                        Thread.sleep(1);
                                } catch (InterruptedException e) {
                                }
                        }
                        return sum;
                }
                // 任务太大,一分为二:
                int middle = (end + start) / 2;
                System.out.println(
                                String.format("split %d~%d ==> %d~%d, %d~%d", start, end, start, middle, middle, end));
                SumTask subtask1 = new SumTask(this.array, start, middle);
                SumTask subtask2 = new SumTask(this.array, middle, end);
                invokeAll(subtask1, subtask2);
                Long subresult1 = subtask1.join();
                Long subresult2 = subtask2.join();
                Long result = subresult1 + subresult2;
                System.out.println("result = " + subresult1 + " + " + subresult2 + " ==> " + result);
                return result;
        }
}